package com.karson.common.dto;

import com.karson.common.vo.HotWorkVo;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Queue;
@Data
@ToString
@NoArgsConstructor
public class TopKDTO {

    private int k = 0;

    private Queue<HotWorkVo> queue;

    public TopKDTO(int k,Queue<HotWorkVo> queue){
        this.k = k;
        this.queue = queue;
    }

    public void add(HotWorkVo hotVideo) {
        if (queue.size() < k) {
            queue.add(hotVideo);
        } else if (queue.peek() != null && queue.peek().getHot() < hotVideo.getHot()) {
            queue.poll();
            queue.add(hotVideo);
        }

        return;
    }


    public List<HotWorkVo> get(){
        final ArrayList<HotWorkVo> list = new ArrayList<>(queue.size());
        while (!queue.isEmpty()) {
            list.add(queue.poll());
        }
        Collections.reverse(list);
        return list;
    }


}